package com.dg11185.nearshop.view.adapter;

import java.util.List;

import com.android.nearshop.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * @link TwoLevelCatalogView 的适配器
 * @author wanliang527
 * @date 2014-4-24
 */
public class SimpleListAdapter extends ArrayAdapter<String> {

    private Context mContext;
    private List<String> mListData;// 标题
    private int selectedPos = 0;
    private Drawable selectedItemBg, normalItemBg;

    public void setSelectedItemBg(Drawable selectedItemBg) {
        this.selectedItemBg = selectedItemBg;
    }

    public void setNormalItemBg(Drawable normalItemBg) {
        this.normalItemBg = normalItemBg;
    }

    private float textSize = -1;
    private OnClickListener onClickListener;
    private OnItemClickListener mOnItemClickListener;

    public SimpleListAdapter(Context context, List<String> listData,
            int itemSelectedBgId, int itemNormalBgId) {
        // super(context,R.string.no_data,listData);
        super(context, 0, listData);
        mContext = context;
        mListData = listData;
        selectedItemBg = mContext.getResources().getDrawable(itemSelectedBgId);
        normalItemBg = mContext.getResources().getDrawable(itemNormalBgId);
        init();
    }

    public SimpleListAdapter(Context context, List<String> listData) {
        this(context, listData, R.color.ex_list_selected,
                R.color.ex_list_normal);
    }

    public SimpleListAdapter(Context context, List<String> listData,
            Drawable selectedItemBg, Drawable normalItemBg) {
        this(context, listData);
        this.selectedItemBg = selectedItemBg;
        this.normalItemBg = normalItemBg;
    }

    private void init() {
        onClickListener = new OnClickListener() {
            @Override
            public void onClick(View view) {
                selectedPos = (Integer) view.getTag();
                setSelectedPosition(selectedPos, true);
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(view, selectedPos);
                }
            }
        };
    }

    /**
     * 设置选中的position,并通知列表刷新
     */
    public void setSelectedPosition(int pos, boolean isDataSetChanged) {
        if (mListData != null && pos < mListData.size()) {
            selectedPos = pos;
            if (isDataSetChanged) {
                notifyDataSetChanged();
            }
        }
    }

    /**
     * 获取选中的position
     */
    public int getSelectedPosition() {
        if (mListData != null && selectedPos < mListData.size()) {
            return selectedPos;
        }
        return 0;
    }

    /**
     * 设置列表字体大小
     */
    public void setTextSize(float tSize) {
        textSize = tSize;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView view;
        if (convertView == null) {
            view = (TextView) LayoutInflater.from(mContext).inflate(
                    R.layout.choose_item, parent, false);
        } else {
            view = (TextView) convertView;
        }
        view.setTag(position);
        // 设置标题
        if (mListData != null && position < mListData.size()) {
            view.setText(mListData.get(position));
        }
        // 设置字号
        if (textSize > 0) {
            view.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        }
        // 设置选中的Item背景
        if (position == selectedPos) {
            view.setBackgroundDrawable(selectedItemBg);
        } else {
            view.setBackgroundDrawable(normalItemBg);
        }
        view.setPadding(20, 0, 0, 0);
        view.setOnClickListener(onClickListener);
        return view;
    }

    public void setOnItemClickListener(OnItemClickListener l) {
        mOnItemClickListener = l;
    }

    /**
     * 重新定义菜单选项单击接口
     */
    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

}
